package com.party.mobile.web.controller.analyze;

import com.party.core.exception.BusinessException;
import com.party.core.service.count.IModuleCountService;
import com.party.core.service.count.impl.ModuleCountFactory;
import com.party.mobile.biz.analyze.MemberMerchantDetailBizService;
import com.party.mobile.web.controller.appPay.AppAliPayController;
import com.party.mobile.web.dto.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 商户统计控制器
 * Created by wei.li.
 *
 * @date 2017/12/20 0020
 * @time 15:00
 */

@Controller
@RequestMapping(value = "member/merchant")
public class MemberMerchantController {

    @Autowired
    private MemberMerchantDetailBizService memberMerchantDetailBizService;

    @Autowired
    private ModuleCountFactory moduleCountFactory;

    protected Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 统计机构的浏览量
     * @param memberId 会员编号
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "countMerchantNum")
    public AjaxResult countMerchantNum(String memberId){
        AjaxResult ajaxResult = new AjaxResult(true);
        try {
            memberMerchantDetailBizService.countViewNum(memberId);
        } catch (Exception e) {
            logger.error("统计机构浏览量数据异常", e);
            ajaxResult.setSuccess(false);
            ajaxResult.setDescription("统计机构浏览量数据异常");
            return ajaxResult;
        }
        return ajaxResult;
    }


    /**
     * 统计业务的浏览量
     * @param id 业务编号
     * @param type 类型
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "countModuleView")
    public AjaxResult countModuleView(String id, String type){
        AjaxResult ajaxResult = new AjaxResult(true);
        try {
            IModuleCountService moduleCountService = moduleCountFactory.getModuleCount(type);
            moduleCountService.addViewNum(id);
        }catch (BusinessException be){
            ajaxResult.setSuccess(false);
            ajaxResult.setDescription(be.getMessage());
            return ajaxResult;
        }
        catch (Exception e) {
            logger.error("统计业务浏览量异常", e);
            ajaxResult.setSuccess(false);
            ajaxResult.setDescription("统计业务浏览量异常");
            return ajaxResult;
        }
        return ajaxResult;
    }
}
